// components/CollectionList/CollectionList.js
import { myRequest } from '../../utils/request';
import api from '../../utils/api'

Component({
    /**
     * 组件的属性列表
     * 由组件外部传入的数据 等同于vue中的props
     */
    properties: {
      type: {
        type: String,
        value: ''  // 展示类型  1：专辑  2：歌单  3：歌手
      },
      hot: {
        type: Boolean,
        value: false   // 是否展示热门数据
      }
    },

    
  observers: {
    'hot': function (hot) {
      if(hot) {
        if(this.properties.type === '1') {
          this.setData({selectCategoryId: '-1'});
          this.getHotAlbumList()
        } else if (this.properties.type === '2') {
          this.setData({selectCategoryId: '-1'});
          this.getHotMusicMenuList()
        } else if (this.properties.type === '3') {
          this.setData({selectCategoryId: '-1'});
          this.getHotSingerList()
        }
      }
    }
  },
  
    /**
     * 组件的初始数据
     */
    data: {
      resultList: [],
      categoryList: [],
      selectCategoryId: '0'
    },

    lifetimes: {
        attached: async function () {
          // 在组件实例进入页面节点树时执行
          if(this.properties.type === '1') {
            // 专辑
            await this.getAlbumCategoryList();
            if (!this.properties.hot) {
              this.getAlbumListByCategoryId('0');
            }
          } else if(this.properties.type === '2') {
            // 歌单
            let categoryList = [
              {name: '全部', id: '0'},
              {name: '热门', id: '-1'},
              {name: '官方', id: '2'},
              {name: '大众', id: '1'}
            ];
            this.setData({categoryList: categoryList});
            if (!this.properties.hot) {
              this.getMusicMenuByIsOfficial('0')
            }
            
           
          } else if (this.properties.type === '3') {
            // 歌手
            let categoryList = [
              {name: '全部', id: '0'},
              {name: '热门', id: '-1'}
            ];
            this.setData({categoryList: categoryList});
            if (!this.properties.hot) {
              this.getAllSingerList();
            }
            
          
          }
          

        },
        detached: function () {
          // 在组件实例被从页面节点树移除时执行
        }
    },

    
  
    /**
     * 组件的方法列表
     */
    methods: {
      goToMusicList(event) {
        const data = event.currentTarget.dataset
        wx.navigateTo({
          url: `/pages/musicList/musicList?type=${this.properties.type}&searchId=${data.id}`
        })
      },
      changeTag(e) {
        this.setData({selectCategoryId: e.currentTarget.id});
        if(this.properties.type === '1') {
          // 专辑
          if(e.currentTarget.id === '-1') {
            // 查热门
            this.getHotAlbumList();
          } else {
            this.getAlbumListByCategoryId(e.currentTarget.id);
          }
         
        } else if (this.properties.type === '2') {
          // 歌单
          if(e.currentTarget.id === '-1') {
            // 查热门
            this.getHotMusicMenuList();
          } else {
            this.getMusicMenuByIsOfficial(e.currentTarget.id);
          }
        } else if (this.properties.type === '3') {
          // 歌手
          if(e.currentTarget.id === '-1') {
            // 查热门
            this.getHotSingerList();
          } else {
            this.getAllSingerList(e.currentTarget.id);
          }
        }
      },

      // 获取热门歌手数据
      async getHotSingerList() {
        const res = await myRequest({
            url: '/app/singer/hot',
            method: 'POST'
        });
        if(res.data.code === 0) {
            let result = res.data.data;
            result.forEach(e => {
              e.show = true;
              e.viewNum = this.tranNumber(e.viewNum, 2);
              e.coverPhoto = api.BASE_URL + '/photo/view?filename=' + e.headPic;
            });
            // 计算填充空位
            let empty = 3 - result.length % 3;
            if(empty < 3) {
              for(let i=0; i<empty; i++) {
                result.push({show: false});
              }
            }
            this.setData({resultList: result});
        }
      },

      // 获取热门专辑数据
      async getHotAlbumList() {
        const res = await myRequest({
            url: '/app/album/hot',
            method: 'POST'
        });
        if(res.data.code === 0) {
            let result = res.data.data;
            result.forEach(e => {
              e.show = true;
              e.viewNum = this.tranNumber(e.viewNum, 2);
              e.coverPhoto = api.BASE_URL + '/photo/view?filename=' + e.coverPhoto;
            });
            // 计算填充空位
            let empty = 3 - result.length % 3;
            if(empty < 3) {
              for(let i=0; i<empty; i++) {
                result.push({show: false});
              }
            }
            this.setData({resultList: result});
        }
      },

      // 获取热门歌单数据
      async getHotMusicMenuList() {
        const res = await myRequest({
            url: '/app/music/menu/hot',
            method: 'POST'
        });
        if(res.data.code === 0) {
            let result = res.data.data;
            result.forEach(e => {
              e.show = true;
              e.viewNum = this.tranNumber(e.viewNum, 2);
              e.coverPhoto = api.BASE_URL + '/photo/view?filename=' + e.coverPhoto;
            });
            // 计算填充空位
            let empty = 3 - result.length % 3;
            if(empty < 3) {
              for(let i=0; i<empty; i++) {
                result.push({show: false});
              }
            }
            this.setData({resultList: result});
        }
      },
      // 根据是否官方歌单来筛选
      async getMusicMenuByIsOfficial(isOfficial) {
        const res = await myRequest({
          url: '/app/music/menu/getByIsOfficial',
          method: 'POST',
          data: {isOfficial: isOfficial}
        });
        if(res.data.code === 0) {
            let result = res.data.data;
            result.forEach(e => {
              e.show = true;
              e.viewNum = this.tranNumber(e.viewNum, 2);
              e.coverPhoto = api.BASE_URL + '/photo/view?filename=' + e.coverPhoto;
            });
            // 计算填充空位
            let empty = 3 - result.length % 3;
            if(empty < 3) {
              for(let i=0; i<empty; i++) {
                result.push({show: false});
              }
            }
            
            this.setData({resultList: result});
        }
      },
      // 查询专辑分类列表
      async getAlbumCategoryList() {
        const res = await myRequest({
            url: '/app/album/category/all',
            method: 'POST'
        });
        if(res.data.code === 0) {
            let result = res.data.data;
            result.unshift({id: '-1', name: '热门'})
            result.unshift({id: '0', name: '全部'})
            this.setData({categoryList: result});
        }
      },
      // 查询全部歌手数据
      async getAllSingerList() {
        const res = await myRequest({
            url: '/app/singer/all',
            method: 'POST'
        });
        if(res.data.code === 0) {
            let result = res.data.data;
            result.forEach(e => {
              e.show = true;
              e.viewNum = this.tranNumber(e.viewNum, 2);
              e.coverPhoto = api.BASE_URL + '/photo/view?filename=' + e.headPic;
            });
            // 计算填充空位
            let empty = 3 - result.length % 3;
            if(empty < 3) {
              for(let i=0; i<empty; i++) {
                result.push({show: false});
              }
            }
            this.setData({resultList: result});
        }
      },
      // 根据专辑分类id获取专辑数据
      async getAlbumListByCategoryId(id) {
        const res = await myRequest({
            url: '/app/album/getByCategoryId',
            method: 'POST',
            data: {albumCategoryId: id}
        });
        if(res.data.code === 0) {
            let result = res.data.data;
            result.forEach(e => {
              e.show = true;
              e.viewNum = this.tranNumber(e.viewNum, 2);
              e.coverPhoto = api.BASE_URL + '/photo/view?filename=' + e.coverPhoto;
            });
            // 计算填充空位
            let empty = 3 - result.length % 3;
            if(empty < 3) {
              for(let i=0; i<empty; i++) {
                result.push({show: false});
              }
            }
         
            this.setData({resultList: result});
        }
      },
      // 数量转换
      tranNumber(num, point) {
        let numStr = num.toString().split('.')[0]
        if (numStr.length < 6) {
          return numStr
        } else if (numStr.length >= 6 && numStr.length <= 8) {
          let decimal = numStr.substring(numStr.length - 4, numStr.length - 4 + point)
          return parseFloat(parseInt(num / 10000) + '.' + decimal) +
            '万'
        } else if (numStr.length > 8) {
          let decimal = numStr.substring(numStr.length - 8, numStr.length - 8 + point)
          return parseFloat(parseInt(num / 100000000) + '.' + decimal) + '亿'
        }
      }
    }
  })
  